package com.smartcruise.data.cruiseData;

import android.location.Location;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.model.LatLng;
import com.smartcruise.data.ReportData;
import com.smartcruise.service.LocationManager;
import com.smartcruise.util.Key;
import com.smartcruise.util.ListUtils;
import com.smartcruise.util.TempleUser;
import com.smartcruise.util.TimeUtils;
import com.smartcruise.util.network.netUtils.NetContract;
import com.smartcruise.util.network.netUtils.Result;
import com.smartcruise.util.schedulerUtil.CronTransfer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;

/**
 * Created by FengChaoQun
 * on 2018/2/27 0027
 * 巡航任务工具类
 */

public class CruiseContract {

    /**
     * 2018/3/11 0011 12:23
     * description:本地复制出来的任务的四种状态
     */
    public static final String TASK_IS_WAIT_START = "待执行";
    public static final String TASK_IS_RUNNING = "进行中";
    public static final String TASK_IS_PAUSE = "已暂停";
    public static final String TASK_IS_END = "已结束";

    /**
     * 2018/3/11 0011 12:24
     * description:节点任务需要提交的数据
     */
    public static final String NODE_EDIT_DATA_TEXT = "文字";
    public static final String NODE_EDIT_DATA_PICTURE = "拍照";
    public static final String NODE_EDIT_DATA_VIDEO = "视频";
    public static final String NODE_EDIT_DATA_AUDIO = "语音";
    public static final String NODE_EDIT_DATA_SCAN = "扫码";

    /**
     * 2018/3/11 0011 12:24
     * description:审核状态
     */
    public static final int UNAUDIT = 0;//未审核
    public static final int AUDITED = 1;//已审核

    /**
     * 2018/3/11 0011 12:25
     * description:审核结果
     */
    public static final String AUDIT_SUCCESS = "通过";
    public static final String AUDIT_FAIL = "驳回";

    /**
     * description:判断一个巡航任务是否已经开始
     */

    public static boolean isStarted(CruiseData cruiseData) {
        return cruiseData.isFromNet() || TASK_IS_RUNNING.equals(cruiseData.getStatusInApp()) || TASK_IS_PAUSE.equals(cruiseData.getStatusInApp())
                || TASK_IS_END.equals(cruiseData.getStatusInApp());
    }

    /**
     * description:判断一个巡航任务当前是否可以修改数据
     * 1.如果已经审核通过 则不可以修改数据
     * 2.如果已经驳回 则可以修改数据
     * 3.如果任务已经开始了 则可以修改数据
     */

    public static boolean isCanModify(CruiseData cruiseData) {
        if (cruiseData.getAuditStatus() == CruiseContract.AUDITED && CruiseContract.AUDIT_SUCCESS.equals(cruiseData.getAuditResult())) {
            return false;
        }
        if (cruiseData.getAuditStatus() == CruiseContract.AUDITED && CruiseContract.AUDIT_FAIL.equals(cruiseData.getAuditResult())) {
            return true;
        }
        return isStarted(cruiseData);
    }

    /**
     * description:开始任务
     */

    public static void startTask(final CruiseData cruiseData, Realm realm) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                cruiseData.setActualRunDate(TimeUtils.currentTime());//设置实际开始时间
                cruiseData.setLastStartTime(cruiseData.getActualRunDate());//设置最近一次开始时间
                cruiseData.setStatusInApp(CruiseContract.TASK_IS_RUNNING);//改变状态为正在运行
            }
        });
        logExecuteTime(cruiseData);
    }

    /**
     * description:暂停任务
     */

    public static void pauseTask(final CruiseData cruiseData, Realm realm) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                cruiseData.addPauseTime();
                cruiseData.setStatusInApp(CruiseContract.TASK_IS_PAUSE);//改变状态为已暂停
            }
        });
        logExecuteTime(cruiseData);
    }

    /**
     * description:重启任务
     */

    public static void restartTask(final CruiseData cruiseData, Realm realm) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                cruiseData.setLastStartTime(TimeUtils.currentTime());
                cruiseData.setStatusInApp(CruiseContract.TASK_IS_RUNNING);//改变状态为正在运行
            }
        });
        logExecuteTime(cruiseData);
    }

    /**
     * description:结束任务
     */

    public static void endTask(final CruiseData cruiseData, Realm realm) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                cruiseData.setActualStopDate(TimeUtils.currentTime());//设置结束时间
                //如果当前任务正在执行 则还需要记录执行时间段
                if (CruiseContract.TASK_IS_RUNNING.equals(cruiseData.getStatusInApp())) {
                    Log.d("CruiseContract", "need add pause time");
                    cruiseData.addPauseTime();
                } else {
                    Log.d("CruiseContract", "getStatusInApp:" + cruiseData.getStatusInApp());
                }
                cruiseData.setStatusInApp(CruiseContract.TASK_IS_END);//改变状态为已结束
            }
        });
        logExecuteTime(cruiseData);
    }

    private static void logExecuteTime(CruiseData cruiseData) {
//        Log.d("CruiseContract", "最初开始时间:" + cruiseData.getActualRunDate());
//        Log.d("CruiseContract", "最近一次开始:" + cruiseData.getLastStartTime());
//        Log.d("CruiseContract", "执行时间段:" + cruiseData.getPauseStartDate());
//        Log.d("CruiseContract", "结束时间:" + cruiseData.getActualStopDate());
    }

    /**
     * description:监测当前是是否在节点的位置
     */

    public static boolean isOnPosition(LatLng latLng, CruiseData cruiseData, NodeTaskListBean nodeTaskListBean) {
        if (latLng == null) {
            return false;
        }
        CoordinateBean coordinateBean = nodeTaskListBean.getPatrollingNodeEntity().getCoordinateList().get(0);
        double distance = LocationManager.getDistance(latLng.latitude, latLng.longitude, coordinateBean.getLatDouble(), coordinateBean.getLngDouble());
        if (TextUtils.isEmpty(cruiseData.getThreshold())) {
            return true;
        } else {
            double threshold = Double.parseDouble(cruiseData.getThreshold());
            return distance <= threshold;
        }
    }

    public static boolean isOnPosition(AMapLocation aMapLocation, CruiseData cruiseData, NodeTaskListBean nodeTaskListBean) {
        if (aMapLocation == null) {
            return false;
        }
        CoordinateBean coordinateBean = nodeTaskListBean.getPatrollingNodeEntity().getCoordinateList().get(0);
        double distance = LocationManager.getDistance(aMapLocation.getLatitude(), aMapLocation.getLongitude(), coordinateBean.getLatDouble(), coordinateBean.getLngDouble());
        if (TextUtils.isEmpty(cruiseData.getThreshold())) {
            return true;
        } else {
            double threshold = Double.parseDouble(cruiseData.getThreshold());
            return distance <= threshold;
        }
    }

    public static boolean isOnPosition(Location location, CruiseData cruiseData, NodeTaskListBean nodeTaskListBean) {
        if (location == null) {
            return false;
        }
        CoordinateBean coordinateBean = nodeTaskListBean.getPatrollingNodeEntity().getCoordinateList().get(0);
        double distance = LocationManager.getDistance(location.getLatitude(), location.getLongitude(), coordinateBean.getLatDouble(), coordinateBean.getLngDouble());
        if (TextUtils.isEmpty(cruiseData.getThreshold())) {
            return true;
        } else {
            double threshold = Double.parseDouble(cruiseData.getThreshold());
            return distance <= threshold;
        }
    }

    /**
     * description:生成指定日期的任务
     */

    public static void buildTask(Calendar calendar, Realm realm) {
        //先查找出所有的原任务
        RealmResults<CruiseData> all = realm.where(CruiseData.class)
                .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                .equalTo(Key.isCopyData, false)
                .equalTo(Key.isFromNet, false)
                .findAll();
        for (CruiseData cruiseData : all) {
            List<Date> dates = CronTransfer.computeFireTimesBetween(cruiseData.getCronExpression(),
                    TimeUtils.getDayBeginTime(calendar), TimeUtils.getDayEndTime(calendar));
            if (ListUtils.isHaveContent(dates)) {
                for (Date date : dates) {//检查任务是否已经存在  如果没有 则创建
                    CruiseData copyCruise = realm.where(CruiseData.class)
                            .equalTo(Key.patrollingTaskId, cruiseData.getPatrollingTaskId())
                            .equalTo(Key.shouldRunDate, date.getTime())
                            .findFirst();
                    if (copyCruise == null) {
                        CruiseContract.copyCruiseData(realm, cruiseData, date.getTime());
                    }
                }
            }
        }
    }

    /**
     * description:从一个源任务检查并复制出当天需要完成的任务
     * 根据该任务当日需要执行的时间创建任务 如果任务已经存在 则不创建
     */

    public static void buildTodayTask(CruiseData cruiseData, Realm realm) {
        List<Date> dates = getTodayTaskDate(cruiseData);
        if (ListUtils.isHaveContent(dates)) {
            for (Date date : dates) {//检查任务是否已经存在  如果没有 则创建
                CruiseData copyCruise = realm.where(CruiseData.class)
                        .equalTo(Key.patrollingTaskId, cruiseData.getPatrollingTaskId())
                        .equalTo(Key.shouldRunDate, date.getTime())
                        .findFirst();
                if (copyCruise == null) {
                    CruiseContract.copyCruiseData(realm, cruiseData, date.getTime());
                }
            }
        }
    }

    /**
     * description:获取指定任务在当天需要执行的时间
     */

    public static List<Date> getTodayTaskDate(CruiseData cruiseData) {
        Calendar calendar = Calendar.getInstance();
        return CronTransfer.computeFireTimesBetween(cruiseData.getCronExpression(),
                TimeUtils.getDayBeginTime(calendar), TimeUtils.getDayEndTime(calendar));
    }

    /**
     * description:更新本地数据库
     * 1.删除掉所有未执行的本地任务
     * 2.重新生成本地任务
     */

    public static void refreshLocalDataBase() {
        long time = TimeUtils.currentTime();
        Log.d("CruiseContract", "数据库更新开始...");
        try (Realm realm = Realm.getDefaultInstance()) {
            //1.先删除本地所有的未执行的复制任务
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    realm.where(CruiseData.class)
                            .equalTo(Key.isCopyData, true)
                            .equalTo(Key.statusInApp, CruiseContract.TASK_IS_WAIT_START)
                            .findAll().deleteAllFromRealm();
                }
            });

            //2.查找出所有的原数据
            final RealmResults<CruiseData> originalDataList = realm.where(CruiseData.class)
                    .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                    .equalTo(Key.isCopyData, false)
                    .equalTo(Key.isFromNet, false)
                    .findAll();

            //生成本地任务
            for (CruiseData originalData : originalDataList) {
                long currentTime = TimeUtils.currentTime();
                buildTodayTask(originalData, realm);
                Log.d("CruiseContract", "生成任务耗时:" + (TimeUtils.currentTime() - currentTime));
            }
        }
        Log.d("CruiseContract", "数据库更新结束");
        Log.d("CruiseContract", "数据更新用时:" + (TimeUtils.currentTime() - time));
    }

    /**
     * description:检查本地数据库中指定原数据对应的复制出来的数据
     * 1.先找出该原数据所有的未执行的复制数据
     * 2.删除掉那些不是当天需要执行的任务
     * 3.更新当天需要执行的任务
     * 注意：这里没有采用先删除所有的复制数据再重新创建 原因是该操作可以在所有界面执行 当用户正在查看一个任务数据的时候
     * 如果数据被删除 会导致界面销毁  为保证用户体验 在这里更新一个数据的时候不采用先删除后重建的方法
     */
    public static void checkOneData(CruiseData originalData) {
        long time = TimeUtils.currentTime();
        try (Realm realm = Realm.getDefaultInstance()) {
            //1.找出指定源任务复制出来的尚未执行的本地任务
            final RealmResults<CruiseData> allCopyData = realm.where(CruiseData.class)
                    .equalTo(Key.patrollingTaskId, originalData.getPatrollingTaskId())
                    .equalTo(Key.isCopyData, true)
                    .equalTo(Key.statusInApp, CruiseContract.TASK_IS_WAIT_START)
                    .findAll();

            List<Date> todayTaskDate = getTodayTaskDate(originalData);

            if (ListUtils.isHaveContent(todayTaskDate)) {
                //获取本地已存在的当天需要执行的任务
                List<CruiseData> todayTaskList = new ArrayList<>();
                for (Date date : todayTaskDate) {
                    CruiseData todayTask = allCopyData.where()
                            .equalTo(Key.patrollingTaskId, originalData.getPatrollingTaskId())
                            .equalTo(Key.shouldRunDate, date.getTime())
                            .findFirst();
                    todayTaskList.add(todayTask);
                }

                //2.删除所有不是当天需要执行的复制任务
                for (final CruiseData data : allCopyData) {
                    if (!todayTaskList.contains(data)) {
                        realm.executeTransaction(new Realm.Transaction() {
                            @Override
                            public void execute(Realm realm) {
                                data.deleteFromRealm();
                            }
                        });
                    }
                }

                //3.更新当天的任务  只更新那些本地不存在或者尚未执行的任务
                for (Date date : todayTaskDate) {
                    //注意：需要在整个数据库中查找 因为需要考虑到已经上传了的任务
                    CruiseData localCopyData = realm.where(CruiseData.class)
                            .equalTo(Key.patrollingTaskId, originalData.getPatrollingTaskId())
                            .equalTo(Key.shouldRunDate, date.getTime())
                            .beginGroup()
                            .equalTo(Key.isFromNet, true)
                            .or().equalTo(Key.isCopyData, true)
                            .endGroup()
                            .findFirst();
                    //如果本地数据存在 且已经上传到服务器了
                    if (localCopyData != null && localCopyData.isFromNet()) {
                        continue;
                    }
                    //如果本地不存在该任务或者该任务尚未开始 则更新任务
                    if (localCopyData == null || CruiseContract.TASK_IS_WAIT_START.equals(localCopyData.getStatusInApp())) {
                        CruiseContract.copyCruiseData(realm, originalData, date.getTime());
                    }
                }

            } else {//如果当天没有需要执行的任务 直接删除所有的复制出来的未执行任务
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        allCopyData.deleteAllFromRealm();
                    }
                });
            }
        }
        Log.d("CruiseContract", "更新单条信息耗时:" + (TimeUtils.currentTime() - time));
    }

    /**
     * description:从一个原数据复制出指定时间执行的任务
     */

    private static void copyCruiseData(Realm realm, CruiseData originalData, long startTime) {
        final CruiseData cruiseData = realm.copyFromRealm(originalData);//复制数据
        cruiseData.setCopyData(true);//添加复制标记
        cruiseData.setShouldRunDate(startTime);//设置应该开始的时间
        cruiseData.setStatusInApp(CruiseContract.TASK_IS_WAIT_START);//设置任务状态为待执行
        cruiseData.buildTempleLocalId();//设置本地临时id
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.copyToRealmOrUpdate(cruiseData);
            }
        });
    }

    /**
     * description:保存已经上传的任务数据
     */

    public static void saveArchiveTasks(final JSONArray patrollingTaskEntityList) throws JSONException {
        if (patrollingTaskEntityList == null || patrollingTaskEntityList.length() == 0) {
            return;
        }
        try (Realm realm = Realm.getDefaultInstance()) {
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    for (int i = 0; i < patrollingTaskEntityList.length(); i++) {
                        try {
                            saveArchiveTask(patrollingTaskEntityList.getJSONObject(i), realm);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }

    /**
     * description:保存通报记录
     */

    public static void saveReportData(Result result) throws JSONException {
        final JSONObject jsonObject = new JSONObject(result.getData());
        final JSONArray reportDataArray = jsonObject.getJSONArray("entityList");
        if (reportDataArray == null || reportDataArray.length() == 0) {
            return;
        }
        try (Realm realm = Realm.getDefaultInstance()) {
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    for (int i = 0; i < reportDataArray.length(); i++) {
                        try {
                            saveOneArchiveReportData(reportDataArray.getJSONObject(i), realm);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }

    /**
     * description:保存单条任务
     * 更新本地的任务 且更新其派生任务
     */

    public static void saveOneNewTask(final Result result) throws JSONException {
        if (!NetContract.isSuccess(result)) {
            return;
        }

        try (Realm realm = Realm.getDefaultInstance()) {
            final JSONObject jsonObject = new JSONObject(result.getData());
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    realm.createOrUpdateObjectFromJson(CruiseData.class, result.getData());
                }
            });
            String id = jsonObject.getString(Key.id);
            CruiseData cruiseData = realm.where(CruiseData.class).equalTo(Key.id, id).findFirst();
            CruiseContract.checkOneData(cruiseData);
        }
    }

//    public static void saveOneAuditedTask(final Result result) throws JSONException {
//        if (!NetContract.isSuccess(result)) {
//            return;
//        }
//
//        //test
//        final JSONObject jsonObject = new JSONObject(result.getData());
//        String id = jsonObject.getString(Key.id);
//        try (Realm realm = Realm.getDefaultInstance()) {
//            CruiseData localData = realm.where(CruiseData.class)
//                    .equalTo(Key.id, id)
//                    .findFirst();
//            if (localData != null && localData.isDataChanged() && localData.getUploadStatus() == 0) {
//                //如果本地有这条数据 且数据已变动且未上传过 则不更新
//                Log.d("CruiseContract", "本地已有相关数据,数据变动且未上传,不予更新");
//            } else {
//                CruiseData cruiseData = realm.createOrUpdateObjectFromJson(CruiseData.class, jsonObject);
//                cruiseData.setFromNet(true);
//                cruiseData.setUploadStatus(0);
//                cruiseData.setDataChanged(false);
//                Log.d("CruiseContract", "更新本地数据成功");
//            }
//        }

    //test

//        try (Realm realm = Realm.getDefaultInstance()) {
//            final JSONObject jsonObject = new JSONObject(result.getData());
//            //任务已经是审核过的
//            realm.executeTransaction(new Realm.Transaction() {
//                @Override
//                public void execute(Realm realm) {
//                    try {
//                        if (CruiseContract.AUDIT_SUCCESS.equals(jsonObject.getString(Key.auditResult))) {
//                            CruiseContract.saveOneCompletedTask(jsonObject, realm);
//                        } else {
//                            CruiseContract.saveOneFailedTask(jsonObject, realm);
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//        }
//    }

    /**
     * description:存储一个服务器上的任务到本地
     * 该方法必须在Realm.Transaction中调用
     */

    public static void saveArchiveTask(JSONObject jsonObject, Realm realm) throws JSONException {
        String id = jsonObject.getString(Key.id);
        CruiseData localData = realm.where(CruiseData.class)
                .equalTo(Key.id, id)
                .findFirst();
        if (localData != null && localData.isDataChanged() && localData.getUploadStatus() == 0) {
            //如果本地有这条数据 且数据已变动且未上传过 则不更新
            Log.d("CruiseContract", "本地已有相关数据,数据变动且未上传,不予更新");
        } else {
            CruiseData cruiseData = realm.createOrUpdateObjectFromJson(CruiseData.class, jsonObject);
            cruiseData.setFromNet(true);
            //重置一下本地状态
            cruiseData.setUploadStatus(0);
            cruiseData.setDataChanged(false);
            //删除本地旧数据
            realm.where(CruiseData.class)
                    .equalTo(Key.isCopyData, true)
                    .equalTo(Key.serverId, cruiseData.getId())
                    .findAll().deleteAllFromRealm();
            Log.d("CruiseContract", "更新本地数据成功");
        }
    }

//    /**
//     * description:存储单条已完成的任务数据到本地数据库
//     * 该方法必须在Realm.Transaction中调用
//     */

//    public static void saveOneCompletedTask(JSONObject data, Realm realm) {
//        //将数据存入本地数据库
//        CruiseData cruiseData = realm.createOrUpdateObjectFromJson(CruiseData.class, data);
//        cruiseData.setCopyData(true);
//        cruiseData.setServerId(cruiseData.getId());
//        cruiseData.setFromNet(true);
//        //删除本地对应的数据
//        realm.where(CruiseData.class)
//                .equalTo(Key.patrollingTaskId, cruiseData.getPatrollingTaskId())
//                .equalTo(Key.shouldRunDate, cruiseData.getShouldRunDate())
//                .equalTo(Key.isCopyData, true)
//                .not().equalTo(Key.id, cruiseData.getId())
//                .findAll().deleteAllFromRealm();
//        Log.d("CruiseContract", "保存审核通过的数据成功");
//    }

//    /**
//     * description:存储单条被驳回的数据到本地数据库
//     * 该方法必须在Realm.Transaction中调用
//     */
//
//    public static void saveOneFailedTask(JSONObject data, Realm realm) throws JSONException {
//        //现在本地数据库查找该数据
//        String id = data.getString(Key.id);
//        CruiseData first = realm.where(CruiseData.class)
//                .equalTo(Key.id, id)
//                .findFirst();
//        //如果本地存在这个数据 并且状态是被驳回、已经修改了数据、还未上传过 则不更新 否则 更新数据
//        if (first != null && first.getAuditStatus() == CruiseContract.AUDITED &&
//                CruiseContract.AUDIT_FAIL.equals(first.getAuditResult()) &&
//                first.isDataChanged() && first.getUploadStatus() == 0) {
//            Log.d("CruiseContract", "id==" + id);
//            Log.d("CruiseContract", "本地对应的驳回数据还未上传 不予更新");
//        } else {
//            //将数据存入本地数据库
//            CruiseData cruiseData = realm.createOrUpdateObjectFromJson(CruiseData.class, data);
//            cruiseData.setCopyData(true);
//            cruiseData.setServerId(cruiseData.getId());
//            //如果不设置 则不会更新 因为服务器返回的json字符串里没有相关属性
//            cruiseData.setUploadStatus(0);
//            cruiseData.setDataChanged(false);
//            //删除本地对应的数据
//            realm.where(CruiseData.class)
//                    .equalTo(Key.patrollingTaskId, cruiseData.getPatrollingTaskId())
//                    .equalTo(Key.shouldRunDate, cruiseData.getShouldRunDate())
//                    .equalTo(Key.isCopyData, true)
//                    .not().equalTo(Key.id, cruiseData.getId())
//                    .findAll().deleteAllFromRealm();
//            Log.d("CruiseContract", "保存被驳回的数据成功");
//        }
//    }

    /**
     * description:存储单条从服务器返回的通报
     * 该方法必须在Realm.Transaction中调用
     */

    public static void saveOneArchiveReportData(JSONObject data, Realm realm) throws JSONException {
        String id = data.getString(Key.id);
        ReportData localData = realm.where(ReportData.class)
                .equalTo(Key.id, id)
                .findFirst();
        //如果本地的数据已经变动且还没有上传 则不更新
        if (localData != null && localData.isDataChanged() && localData.getUploadStatus() == 0) {
            Log.d("CruiseContract", "通报:" + id + "--已修改且尚未上传，不予更新");
        } else {
            ReportData reportData = realm.createOrUpdateObjectFromJson(ReportData.class, data);
//            reportData.setServerId(reportData.getId());
            reportData.setFromNet(true);//设置数据从服务器来的
            //重置一下本地状态
            reportData.setUploadStatus(0);
            reportData.setDataChanged(false);
            //删除本地旧数据
            realm.where(ReportData.class)
                    .equalTo(Key.serverId, reportData.getId())
                    .equalTo(Key.isFromNet, false)
                    .findAll().deleteAllFromRealm();
            Log.d("CruiseContract", "更新通报:" + reportData.getName() + "--成功");
        }
    }
}
